package View;

import Controller.CarController;
import pojo.Car;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

//车辆添加界面
public class CarAdd_window extends Component {
    private final JFrame frame = new JFrame("添加窗口");
    private final JPanel addPanel = new JPanel(); // 面板

    private CarController CarController;

    {
        try {
            CarController = new CarController();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private final JButton saveButton = new JButton("保存");
    private final JButton backButton = new JButton("返回");

    private final JLabel nameLabel = new JLabel("车辆型号:");
    private final JTextField nameTextField = new JTextField(20);
    private final JLabel brandLabel = new JLabel("车辆品牌:");
    private final JTextField brandTextField = new JTextField(20);
    private final JLabel carNumberLabel = new JLabel("车牌号:");
    private final JTextField carNumberTextField = new JTextField(20);
    private final JLabel moneyLabel = new JLabel("租金:");
    private final JTextField moneyTextField = new JTextField(20);
    private final JLabel oilLabel = new JLabel("燃油类型:");
    private final JTextField oilTextField = new JTextField(20);
    private final JLabel colorLabel = new JLabel("车辆颜色:");
    private final JTextField colorTextField = new JTextField(20);
    private final JLabel mileageLabel = new JLabel("车辆里程数:");
    private final JTextField mileageTextField = new JTextField(20);

    public CarAdd_window() {
        // 添加窗口关闭事件监听器
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                // 这里可以添加你的关闭逻辑
                frame.setVisible(false);
                try {
                    new Car_window();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        });
        frameControl();
    }

    // 设置容器管理
    public void frameControl() {
        // 设置底层容器和布局管理器
        frame.setSize(400, 300);
        frame.setLocation(500, 300);
        frame.setResizable(false);

        // 设置信息面板
        JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new GridLayout(7, 2, 0, 10));
        infoPanel.add(nameLabel);
        infoPanel.add(nameTextField);
        infoPanel.add(brandLabel);
        infoPanel.add(brandTextField);
        infoPanel.add(carNumberLabel);
        infoPanel.add(carNumberTextField);
        infoPanel.add(moneyLabel);
        infoPanel.add(moneyTextField);
        infoPanel.add(oilLabel);
        infoPanel.add(oilTextField);
        infoPanel.add(colorLabel);
        infoPanel.add(colorTextField);
        infoPanel.add(mileageLabel);
        infoPanel.add(mileageTextField);

        // 设置按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
//        buttonPanel.add(editButton);
        buttonPanel.add(saveButton);
        buttonPanel.add(backButton);

        // 设置整体布局
        frame.setLayout(new BorderLayout());
        frame.add(infoPanel, BorderLayout.CENTER);
        frame.add(buttonPanel, BorderLayout.SOUTH);

        // 设置按钮事件处理
        setButtonActions();

        // 设置窗口可见性和关闭操作
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    }

    // 设置按钮事件处理
    public void setButtonActions() {
        // 返回保存修改按钮事件处理
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取修改的数据
                String username = nameTextField.getText();//姓名
                String brand = brandTextField.getText();//品牌
                String carNumber = carNumberTextField.getText();//车牌号
                String color = colorTextField.getText();//车辆颜色
                String oil = oilTextField.getText();//燃油类型

                int rental_rate = 0;
                String money = moneyTextField.getText();//租金
                int car_mileage = 0;
                String mileage = mileageTextField.getText();//车辆里程数

                //转换int
                try {
                    rental_rate = Integer.parseInt(money); // 尝试将租金字符串转换为整数
                    car_mileage = Integer.parseInt(mileage); // 尝试将里程数字符串转换为整数
                } catch (Exception ex) {
                    // 处理转换失败的情况，比如给出错误消息或者设置默认值
                    System.err.println("前端转换类型错误: ");
                    // 设置默认值
                    rental_rate = 0;
                    car_mileage = 0; // 默认值
                }

                // 保存修改的逻辑
                // 更新用户信息到数据库
                Car Car = new Car();
                Car.setModel(username);
                Car.setBrand(brand);
                Car.setLicense_plate(carNumber);
                Car.setRental_rate(rental_rate);
                Car.setFuel_type(oil);
                Car.setColor(color);
                Car.setMileage(car_mileage);

                Boolean flag = null;
                try {
                    //添加车辆信息到数据库是否成功
                    flag = CarController.addCar(Car);
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                System.out.println("flag:"+flag);
                if (flag){
                    //保存成功的信息
                    JOptionPane.showMessageDialog(CarAdd_window.this, "保存成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                    // 返回主窗口
                    frame.setVisible(false);
                    try {
                        new Car_window();
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                }else {
                    //保存失败的信息
                    JOptionPane.showMessageDialog(CarAdd_window.this, "保存失败！", "失败", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        // 返回主窗口
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 返回主窗口
                frame.setVisible(false);
                try {
                    new Car_window();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                CarAdd_window addWindow = new CarAdd_window();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }
}
